package com.mediatek.factorymode.touchscreen1;

import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import com.mediatek.factorymode.R;

import java.io.IOException;

public class DrawRectView
        extends SurfaceView
        implements SurfaceHolder.Callback
{
    private Canvas canvas = null;
    private int height;
    private SurfaceHolder mHolder = null;
    private MyRect[][] mHollowRect = new MyRect[4][];
    private Paint mPaint = null;
    private int mStep = 80;
    private int width;

    private class MyRect
    {
        boolean hasFilled;
        Rect rect;

        MyRect() {}
    }

    public DrawRectView(Context paramContext, AttributeSet paramAttributeSet)
    {
        super(paramContext, paramAttributeSet);
        initPaint();
        this.mHolder = getHolder();
        this.mHolder.addCallback(this);
    }

    public static int  gcd(int a,int b)
    {
        int t;

        if (a < b) {
            t  = b;
            b  = a;
            a  = t;
        }
        t  = b/10 ;

        if(b == 1440)
            t = 160;

        return t;
    }
    private void initHollowRects()
    {
        this.width = getMeasuredWidth();
        this.height = getMeasuredHeight();
        this.mStep = this.gcd(this.width, this.height);

        int k = this.height / this.mStep;
        int j = this.width / this.mStep - 2;

        if (this.height > k * this.mStep) {
            if (this.height - k * this.mStep >= this.mStep / 2)
                k += 1;
        }
        if (this.width > (j + 2) * this.mStep) {
            if (this.width - (j + 2) * this.mStep >= this.mStep / 2)
                j += 1;
        }

        this.mHollowRect[0] = new MyRect[k];
        this.mHollowRect[1] = new MyRect[k];
        this.mHollowRect[2] = new MyRect[j];
        this.mHollowRect[3] = new MyRect[j];

        int i = 0;
        while (i < k)
        {
            this.mHollowRect[0][i] = new MyRect();
            this.mHollowRect[1][i] = new MyRect();
            if (i + 1 == k) {
                this.mHollowRect[0][i].rect = new Rect(0, this.mStep * i, this.mStep, this.height);
                this.mHollowRect[1][i].rect = new Rect(this.width - this.mStep, this.mStep * i, this.width, this.height);
            } else {
                this.mHollowRect[0][i].rect = new Rect(0, this.mStep * i, this.mStep, this.mStep * (i + 1));
                this.mHollowRect[1][i].rect = new Rect(this.width - this.mStep, this.mStep * i, this.width, this.mStep * (i + 1));
            }
            i += 1;
        }

        i = 0;
        while (i < j)
        {
            this.mHollowRect[2][i] = new MyRect();
            this.mHollowRect[3][i] = new MyRect();
            if (i + 1 == j) {
                this.mHollowRect[2][i].rect = new Rect(this.mStep * (i + 1), 0, this.width - this.mStep, this.mStep);
                this.mHollowRect[3][i].rect = new Rect(this.mStep * (i + 1), (k-1) * this.mStep, this.width - this.mStep, this.height);
            } else {
                this.mHollowRect[2][i].rect = new Rect(this.mStep * (i + 1), 0, this.mStep * (i + 2), this.mStep);
                this.mHollowRect[3][i].rect = new Rect(this.mStep * (i + 1), (k-1) * this.mStep, this.mStep * (i + 2), this.height);
            }
            i += 1;
        }
    }

    private void checkFillAll()
    {
        int i = 0;
        while (i < this.mHollowRect.length)
        {
            MyRect[] arrayOfMyRect = this.mHollowRect[i];
            int k = arrayOfMyRect.length;
            int j = 0;
            while (j < k)
            {
                if (!arrayOfMyRect[j].hasFilled) {
                    return;
                }
                j += 1;
            }
            i += 1;
        }
        getContext().sendBroadcast(new Intent("com.mediatek.factorymode.touchscreen.test.action.finish").putExtra("TestPass", 2));
    }

    private void drawHollowRects()
    {
        this.canvas = this.mHolder.lockCanvas();
        if (this.canvas != null)
        {
            int i = 0;
            while (i < this.mHollowRect.length)
            {
                MyRect[] arrayOfMyRect = this.mHollowRect[i];
                int k = arrayOfMyRect.length;
                int j = 0;
                while (j < k) {
                    MyRect localMyRect = arrayOfMyRect[j];
                    if (localMyRect.hasFilled) {
                        this.mPaint.setStyle(Paint.Style.FILL);
                    } else {
                        this.mPaint.setStyle(Paint.Style.STROKE);
                    }
                    this.canvas.drawRect(localMyRect.rect, this.mPaint);
                    j += 1;

                }
                i += 1;
            }
            this.canvas.drawLine(this.width - 1, 0.0F, this.width, this.height, this.mPaint);
            this.canvas.drawLine(0.0F, this.height - 1, this.width, this.height, this.mPaint);
            this.mHolder.unlockCanvasAndPost(this.canvas);
        }
    }


    private void initPaint()
    {
        this.mPaint = new Paint();
        this.mPaint.setAntiAlias(false);
        this.mPaint.setColor(-16711936);
        this.mPaint.setStyle(Paint.Style.STROKE);
    }

    public boolean onTouchEvent(MotionEvent event)
    {
        int action = event.getAction();
        if ((action == MotionEvent.ACTION_DOWN) || (action == MotionEvent.ACTION_MOVE))
        {
            int dx = (int)event.getX();
            int dy = (int)event.getY();
            int i = 0;
            while (i < this.mHollowRect.length)
            {
                MyRect[] arrayOfMyRect  = this.mHollowRect[i];
                int j = 0;
                while (j < arrayOfMyRect.length)
                {
                    MyRect myRect = arrayOfMyRect[j];
                    if (myRect.rect.contains(dx, dy)) {
                        myRect.hasFilled = true;
                    }
                    j += 1;
                }
                i += 1;
            }
            drawHollowRects();
            checkFillAll();
        }
        return true;
    }

    public void surfaceChanged(SurfaceHolder paramSurfaceHolder, int paramInt1, int paramInt2, int paramInt3) {}

    public void surfaceCreated(SurfaceHolder paramSurfaceHolder)
    {
        initHollowRects();
        drawHollowRects();
    }

    public void surfaceDestroyed(SurfaceHolder paramSurfaceHolder) {}

}
